package ${package.Controller};

import com.zgiot.zx.common.dto.PageRspDTO;
import com.zgiot.zx.common.dto.Ret;
import com.zgiot.zx.common.page.MyPageInfo;
import com.zgiot.zx.common.page.PageEntity;
import com.zgiot.zx.common.page.PageKit;
import com.zgiot.zx.common.web.BaseController;
import ${package.Entity}.*;
import ${package.Service}.*;
import ${package.Dto}.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@Api(value = "/${entity?uncap_first}", tags = "${table.comment}接口")
@RestController
@RequestMapping("/${entity?uncap_first}")
@Validated
public class ${entity}Controller extends BaseController {
    @Autowired
    private ${entity}Service ${entity?uncap_first}Service;

    @ApiOperation(value = "创建${table.comment}", notes = "创建${table.comment}")
    @PostMapping(value = "/create")
    public Ret<${entity}CreateRspDTO> create(
            @RequestBody
                    ${entity}CreateReqDTO req
    ) {
        ${entity} ${entity?uncap_first} = req.convertTo(${entity}.class);

        ${entity} result = ${entity?uncap_first}Service.create(${entity?uncap_first});

        ${entity}CreateRspDTO ret = result.convertTo(${entity}CreateRspDTO.class);

        return Ret.ok().setData(ret);
    }

    @ApiOperation(value = "更新${table.comment}", notes = "更新${table.comment}")
    @PostMapping(value = "/update")
    public Ret<${entity}UpdateRspDTO> update(
            @RequestBody
                    ${entity}UpdateReqDTO req
    ) {
        ${entity} ${entity?uncap_first} = req.convertTo(${entity}.class);

        ${entity} result = ${entity?uncap_first}Service.update(${entity?uncap_first});

        ${entity}UpdateRspDTO ret = result.convertTo(${entity}UpdateRspDTO.class);

        return Ret.ok().setData(ret);
    }

    @ApiOperation(value = "删除${table.comment}", notes = "删除${table.comment}")
    @PostMapping(value = "/delete/{id}")
    public Ret<${entity}UpdateRspDTO> delete(@NotNull @PathVariable("id") Long id) {
        ${entity?uncap_first}Service.delete(id);

        return Ret.ok();
    }

    @ApiOperation(value = "查询单个${table.comment}", notes = "查询单个${table.comment}")
    @GetMapping(value = "/get/{id}")
    public Ret<${entity}GetRspDTO> get(@NotNull @PathVariable("id") Long id) {
        ${entity} result = ${entity?uncap_first}Service.get(id);

        ${entity}GetRspDTO ret = result.convertTo(${entity}GetRspDTO.class);

        return Ret.ok().setData(ret);
    }


    @ApiOperation(value = "${table.comment}列表查询", notes = "${table.comment}列表查询")
    @GetMapping(value = "/list")
    public Ret<List<${entity}GetListRspDTO>> getList(${entity}GetListReqDTO req) {
        ${entity} ${entity?uncap_first} = req.convertTo(${entity}.class);

        List<${entity}> ret = ${entity?uncap_first}Service.getList(${entity?uncap_first});

        return Ret.ok().setData(ret);
    }

    @ApiOperation(value = "${table.comment}列表分页查询", notes = "${table.comment}列表分页查询")
    @GetMapping(value = "/pagelist")
    public Ret<PageRspDTO<${entity}GetListRspDTO>> getPageList(
            ${entity}GetPageListReqDTO req
    ) {
        ${entity} ${entity?uncap_first} = req.convertTo(${entity}.class);

        MyPageInfo<${entity}> pageResult = ${entity?uncap_first}Service.getPagelist(new PageEntity(req.getPageNum(), req.getPageSize()), ${entity?uncap_first});

        PageRspDTO<${entity}GetListRspDTO> ret = PageKit.convertToPageListRetDTO(pageResult, ${entity}GetListRspDTO.class);

        return Ret.ok().setData(ret);
    }
}
